import 'package:get/get.dart';
import 'package:nyh_motorcade_app/constant/constant.dart';
import 'package:nyh_motorcade_app/entity/cash_flow_details_entity.dart';
import 'package:nyh_motorcade_app/event/update_message_state_event.dart';
import 'package:nyh_motorcade_app/http/http_address.dart';
import 'package:nyh_motorcade_app/http/http_manager.dart';
import 'package:nyh_motorcade_app/utils/event_bus_utils.dart';
import 'package:nyh_motorcade_app/utils/sp_utils.dart';
import 'package:nyh_motorcade_app/utils/toast_utils.dart';
import 'package:nyh_motorcade_app/widgets/multi_state/multi_state_widget.dart';

class CashFlowDetailsController extends GetxController {
  MultiState multiState = MultiState.loading;
  String title = "";
  String cashFlowId = "";
  String cashFlowType = "";
  CashFlowDetailsEntity? cashFlowDetailsEntity;

  //是否更新未读消息状态
  bool isUpdateMessageState = false;
  String messageId = "";

  @override
  void onReady() {
    super.onReady();
    dynamic arguments = Get.arguments;
    if (arguments != null && arguments is Map) {
      cashFlowId = arguments[Constant.PARAM_CASH_FLOW_ID] ?? "";
      cashFlowType = arguments[Constant.PARAM_CASH_FLOW_TYPE] ?? "";
      isUpdateMessageState = arguments[Constant.PARAM_IS_UPDATE_MESSAGE_STATE] ?? false;
      if (isUpdateMessageState) {
        messageId = arguments[Constant.PARAM_MESSAGE_ID] ?? "";
      }
      setTitle();
      getCashFlowDetails();
    }
  }

  Future<void> getCashFlowDetails() async {
    httpManager.post(
      Get.context,
      getHttpPath(),
      {
        "superId": await SPUtils.getString(SPUtils.COMPANY_ID),
        "id": cashFlowId,
      },
      isToast: true,
      onSuccess: (data) {
        if (data == null) {
          ToastUtils.toast("请求失败");
          multiState = MultiState.fail;
          update();
          return;
        }
        cashFlowDetailsEntity = CashFlowDetailsEntity().fromJson(data);
        multiState = MultiState.success;
        update();

        updateMessageState();
      },
      onFail: (code, msg) {
        multiState = MultiState.fail;
        update();
      },
      onError: (msg) {
        multiState = MultiState.error;
        update();
      },
    );
  }

  void onRetry() {
    multiState = MultiState.loading;
    update();
    getCashFlowDetails();
  }

  void setTitle() {
    switch (cashFlowType) {
      case Constant.COMPANY_SETTLEMENT_OUT:
        title = "付款详情";
        break;
      case Constant.COMPANY_SETTLEMENT_IN:
        title = "收款详情";
        break;
      case Constant.ROLL_IN:
        title = "拨入详情";
        break;
      case Constant.ROLL_OUT:
        title = "拨出详情";
        break;
      case Constant.ORDER_PAYMENT:
        title = "订单详情";
        break;
      case Constant.OTHER:
        title = "订单详情";
        break;
      case Constant.ACCOUNT_RECHARGE:
        title = "充值详情";
        break;
      default:
    }
    update();
  }

  String getHttpPath() {
    switch (cashFlowType) {
      case Constant.COMPANY_SETTLEMENT_OUT:
      case Constant.COMPANY_SETTLEMENT_IN:
      case Constant.ROLL_IN:
      case Constant.ROLL_OUT:
        return HttpAddress.GET_CASH_FLOR_DETAILS_BY_TRANSFER_SETTLEMENT;
      case Constant.ORDER_PAYMENT:
      case Constant.OTHER:
        return HttpAddress.GET_CASH_FLOR_DETAILS_BY_ORDER;
      case Constant.ACCOUNT_RECHARGE:
        return HttpAddress.GET_CASH_FLOR_DETAILS_BY_RECHARGE;
      default:
        return "";
    }
  }

  void updateMessageState() {
    if (isUpdateMessageState) {
      EventBusUtils.fire(UpdateMessageState(messageId));
    }
  }
}
